/*
 * @Author: 姚潘涛
 * @Date: 2024-04-08 20:36:39
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-04-10 09:23:01
 * @FilePath:/xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_ui/include/xict_ui_core/imagemanage.h
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#ifndef IMAGECANAGE_H
#define IMAGECANAGE_H

#include "xict_proto/common/lane_line.pb.h"
#include "xict_proto/sensor/can.pb.h"
#include "xict_proto/sensor/image.pb.h"
#include "xict_ui_common/utils/utils.h"
#include "xict_ui_driver/utils/baseimgsdk.h"
#include <QImage>
#include <QMutex>
#include <QThread>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
namespace xict_ui_core {

    class ImageManager : public QThread {
        Q_OBJECT
        BJ_PROPERTY(int, camera_changed);
        BJ_PROPERTY(int, vehicle_type);   // 0: 默认没有 1: 平地机 2:装载机
        //    BJ_PROPERTY(int, show_state); // 0:默认  1:开机动画 2:车辆展示动画
        //    3:视野检查 4:驾驶
        BJ_PROPERTY(
            int,
            view_state);   // 视野检查状态: // 0:前视 1:后视角  2:左视角
                           // 3右视角 4:前轮视野 5:铲刀前视 6:铲刀后视  7:环视
        Q_PROPERTY(int show_state READ get_show_state WRITE set_show_state
                       NOTIFY showStateChanged);
        BJ_PROPERTY(bool, front_line_active);   //
        BJ_PROPERTY(bool, back_line_active);

    public:
        int get_show_state() {
            return m_show_state;
        }
        void set_show_state(int state) {
            if (m_show_state == state)
                return;

            if (m_show_state == 2 && state == 3) {
                EnableSdk();
            } else if (m_show_state == 3 && state == 2) {
                DisableSdk();
            }
            m_show_state = state;
        }

    public:
        ImageManager(QObject* parent = nullptr);

        void init();
        void run() override;
        Q_INVOKABLE void EnableSdk();
        Q_INVOKABLE void DisableSdk();

        void FrontImageCallback(const char*, const xcmg_proto::OpencvImage&);
        void FrontWheelImageCallback(const char*,
                                     const xcmg_proto::OpencvImage&);
        void BackImageCallback(const char*, const xcmg_proto::OpencvImage&);
        void LeftImageCallback(const char*, const xcmg_proto::OpencvImage&);
        void RightImageCallback(const char*, const xcmg_proto::OpencvImage&);
        void FrontBladeImageCallback(const char*,
                                     const xcmg_proto::OpencvImage&);
        void BackBladeImageCallback(const char*,
                                    const xcmg_proto::OpencvImage&);
        void OverLookImageCallback(const char*, const xcmg_proto::OpencvImage&);
        void BladeMRImageCallback(const char*, const xcmg_proto::OpencvImage&);
        void FrontLineImageCallback(const char*,
                                    const xcmg_proto::OpencvImage&);
        void BackLineImageCallback(const char*, const xcmg_proto::OpencvImage&);
    signals:
        void signalAnimationImage(QImage image);   // 开机动画数据
        void signalCurrentImage(int show_state, QImage image);   // 当前图像数据
        void signalFrontImage(int show_state, QImage image);   // 前视图像数据
        void signalFrontWheelImage(int show_state,
                                   QImage image);   // 前轮图像数据
        void signalBackImage(int show_state, QImage image);   // 倒车图像数据
        void signalLeftImage(int show_state, QImage image);   // 左转图像数据
        void signalRightImage(int show_state, QImage image);   // 右转图像数据
        void signalFrontBladeImage(int show_state,
                                   QImage image);   // 铲刀前图像数据
        void signalBackBladeImage(int show_state,
                                  QImage image);   // 铲刀后图像数据
        void signalOverLookImage(int show_state,
                                 QImage image);   // 环视图像数据
        void signalBladeMRImage(int show_state,
                                QImage image);   // 铲刀三维动画数据
        void showStateChanged(int state);

    private:
        int m_show_state;
        cv::Mat m_frame;
        cv::VideoCapture m_cap;
        QString m_boot_url;
        bool do_one_time_;
        QImage m_front_line_image;
        QImage m_back_line_image;
        QMutex mutex;   // 定义一个互斥锁
                        // private:
                        //    QImage m_front;
                        //    QImage m_back;
                        //    QImage m_frontWheel;
                        //    QImage m_left;
                        //    QImage m_right;
                        //    QImage m_bladeBack;
                        //    QImage m_bladeFront;
                        //    QImage m_overlook;
                        //    QImage m_bladeMr;

        //    bool is_main_view =  false;
        //    bool is_play = false;
    };

}   // namespace xict_ui_core

#endif   // IMAGECANAGE_H
